############## 
############## 
############## Models
remove(list = ls())

base::library(conflicted)
base::library(tidyverse)
conflict_prefer("filter","dplyr")
base::library(ggplot2)
base::library(dplyr)
base::library(here)
conflict_prefer("here", "here")
base::library(systemfit)
base::library(stargazer)
base::library(DataCombine)
base::library(haven)
base::library(nnet)
base::library(ggeffects)

load(here("Data", "IndLevel_Data.RData"))

ces_data <- ces_data %>%
  mutate(party_id3 = ifelse(pid7 == 1 | pid7 == 2 | pid7 == 3, 
                            "Democrat", NA),
         party_id3 = ifelse(pid7 == 4, "Independent", party_id3),
         party_id3 = ifelse(pid7 == 5 | pid7 == 6 | pid7 == 7, 
                            "Republican", party_id3)) %>% 
  mutate(party_id3 = factor(party_id3,
                            levels = c("Democrat", "Independent",
                                       "Republican")))


summary(ind_model <- multinom(party_id3 ~ death_rate + unemp_rate + defl_pcincome +
                                log_pop + rural_urban + defl_pcincome + 
                                gender + black + hispanic + asian + other_race + 
                                log_age + college + income + year_elec, 
                              data = ces_data, weights = weight_cumulative))


summary(ces_data$death_rate)
model_pid3 <- ind_model %>% 
  ggeffect(terms = c("death_rate[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]"), 
           ci.lvl = 0.90)

glimpse(model_pid3)
table(model_pid3$response.level)

model_drugs <- data.frame(model_pid3)
glimpse(model_drugs)
#rep_vote <- model_drugs %>% filter(response.level == "Rep")


party_id <- model_drugs %>% 
  mutate(response.level = factor(response.level,
                        levels = c("Democrat", 
                                   "Independent",
                                   "Republican"),
                        labels = c("Democrats", 
                                   "Independents",
                                   "Republicans"))) %>% 
  ggplot(aes(shape = response.level, color = response.level)) + 
  geom_pointrange(aes(x = x, y = predicted, 
                      ymin = conf.low, ymax = conf.high),
                  lwd = .85#, position = position_dodge(width = .5),
                  #color = "darkslategray"
  ) + 
  theme_light() + 
  labs(x = "Overdose Death Rate", y = "Predicted Probability",
       title = NULL) +
  ggtitle(NULL) +
  scale_shape_manual(name = "Party Identification:",
                     labels = c("Democrats", "Independents", "Republicans"),
                     values = c(15, 16, 17)) +
  scale_color_manual(name = "Party Identification:",
                     labels = c("Democrats", "Independents", "Republicans"),
                     values = c("blue", "gray40", "red")) +
  scale_x_continuous(breaks = c(0, 20, 40, 60, 80, 100, 120), lim = c(-1, 121)) +
  #scale_y_continuous(breaks = c(0, 0.2, 0.4, 0.6, 0.8, 1), lim = c(0, 1)) +
  theme(axis.text.x = element_text(hjust = 0.5, size = 11),
        axis.text.y = element_text(size = 11),
        axis.title.y = element_text(size = 12),
        legend.text = element_text(size = 12),
        legend.title = element_text(size = 12),
        legend.position = "bottom",
        legend.direction = "horizontal",
        plot.title = element_text(size = 14))

party_id


